<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class CoachController extends HomebaseController {
    //添加新教练
	public function addcoach() {
		$this->display(":addcoach");
		if($_POST['coachname']&&$_POST['description']){
			$arr['coachname']=$_POST['coachname'];
			$arr['description']=$_POST['description'];
			$Coach=M('coach');
			$msg=$Coach->add($arr);
			//图片
			if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/png")|| ($_FILES["file"]["type"] == "image/bmp")) && ($_FILES["file"]["size"] < 204800)) //定义文件类型，大小为200k以内
			{
				if ($_FILES["file"]["error"] > 0) 
				{ 
					$m= false;
				} else { 
					$format='.'.substr($_FILES["file"]["type"],6);
					if(move_uploaded_file($_FILES["file"]["tmp_name"],UPLOAD.'coach'.$msg.$format)){
						$data2['coachpic']=urlencode(UPLOAD.'coach'.$msg.$format);
						$xx=$Coach->where("coachid='$msg'")->save($data2);
						$m=true;
					}else{
						$m=false;
					}
				}
			}else{
				$m=true;
			} 
			if($msg){
				$this->succ('showcoach');
			}
			else{
				$this->err('showcoach');
			}
		}
		else{
			echo '请添加信息！';
		}
	}
	public function clear(){
		$_POST=null;
		session("coachwhere['coachname']",null);
		$this->showcoach();
	}
	//教练列表（修改、删除）
	public function showcoach() {
		$Coach=M('coach');
		session(array('name'=>'session_id','expire'=>3600));
		if($_POST['coachname']){
			$where['coachname']=$_POST['coachname'];
		}
		$count=$Coach->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Coach
		->where($where)
		->limit($page->firstRow , $page->listRows)
		->select();
		$this->assign('msg',$msg)
		->assign('where',$where)
		->assign("page", $page->show('Admin'));
    	$this->display(":showcoach");
    }
	//删除教练
	public function delcoach() {
		$coachid=$_GET['coachid'];
		$Coach=M('coach');
		$msg=$Coach->where("coachid='$coachid'")->delete();//where语句中的变量需要引号
		if($msg){
			$this->succ('showcoach');
		}
	}
	//编辑教练
	public function editcoach() {
			$Coach=M('coach');
			$coachid=$_GET['coachid'];
			$msg1=$Coach->where("coachid='$coachid'")->select();
			$msg1[0]['coachpic']=urldecode($msg1[0]['coachpic']);
			$this->assign('msg',$msg1);
			$this->display(":editcoach");
			if($_POST['coachname']&&$_POST['description']){
				$arr['coahname']=$_POST['coachname'];
				$arr['description']=$_POST['description'];
				$Coach=M('coach');
				$msg=$Coach->where("coachid='$coachid'")->save($arr);
				//图片
				if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png")|| ($_FILES["file"]["type"] == "image/bmp")) && ($_FILES["file"]["size"] < 204800))
				{
					if ($_FILES["file"]["error"] > 0) 
					{ 
						$m= false;
					} else { 
						$format='.'.substr($_FILES["file"]["type"],6);
						if(move_uploaded_file($_FILES["file"]["tmp_name"],UPLOAD.'coach'.$coachid.$format)){
							$data2['coachpic']=urlencode(UPLOAD.'coach'.$coachid.$format);
							$xx=$Coach->where("coachid='$coachid'")->save($data2);
							$m=true;
						}else{
							$m=false;
						}
					}
				}else{
					$m=false; 
				} 
				if($m){
					echo "<img src=$msg1[0]['coachpic'] />";
					$this->succ('showcoach');
				}
				else{
					$this->err('showcoach');
				}
			}
	}
	public function showjudge(){
		$Judge=M('judge');
		$Coach=M('coach');
		$Member=M('member');
		$where['coachid']=$_GET['coachid'];
		$coachname=$Coach->where($where)->getField('coachname');
		$coachpic=$Coach->where($where)->getField('coachpic');
		$coachpic=urldecode($coachpic);
		$count=$Judge->where($where)->count();
		$page=$this->page($count,10);
		$msg=$Judge
		->where($where)
		->order('judgetime desc')
		->limit($page->firstRow , $page->listRows)
		->select();
		for($i=0;$i<count($msg);$i++){
			$where2['uid']=$msg[$i]['uid'];
			$msg[$i]['name']=$Member->where($where2)->getField('name');
			$msg[$i]['tel']=$Member->where($where2)->getField('tel');
		}
		$this->assign('msg',$msg)
		->assign("page", $page->show('Admin'))
		->assign('coachname',$coachname)
		->assign('coachpic',$coachpic);
		$this->display(':showjudge');
	}
	public function deljudge(){
		$where['judgeid']=$_GET['judgeid'];
		$Judge=M('judge');
		$msg=$Judge->where($where)->delete();
		if($msg){
			$this->succ('showjudge');
		}else{
			$this->err('showjudge');
		}
		}
	//操作成功
	public function succ($m){
		$C='Coach';
		$this->assign('m',$m)->assign('C',$C);
		$this->display(':success');
	}
	//操作失败
	public function err($m){
		$C='Coach';
		$this->assign('m',$m)->assign('C',$C);		
		$this->display(':err');
	}
}
